// pages/show/show.js
import Notify from '@vant/weapp/notify/notify';
const app = getApp()
import Toast from '@vant/weapp/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userid: '',
    id: '',
    user: {},
    offset: 0,
    score: Date.parse(new Date()),
    dynamics: [],
    count: {},
    followed: '',
    loadModal:true
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      id: options.id
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    var userid = wx.getStorageSync('user').id;
    this.setData({
      score: Date.parse(new Date()),
      userid: userid
    })
    //获取该id的用户信息
    app.request({
      url: app.globalData.apiUrl + "/user/get/" + this.data.id,
      method: 'GET',
    }).then(res => {
      if (res.statusCode == 200) {
        this.setData({
          user: res.data.data,
          loadModal:false
        })
        console.log(this.data.user)
      } else {
        this.setData({
          loadModal:false
        })
        Notify('服务错误，退出重试');
      }
    })
    //获取所有动态,或者分页获取
    this.getDynamicsByOffsetAndScoreAndId()
    //获取count
    this.getCount()
    //获取是否关注
    this.isFollowed()
  },
  //下滑获取动态
  onReachBottom: function () {
    if (this.data.score == null || '' || undefined) {
      console.log('没有过呢更多啦')
    } else {
      this.getDynamicsByOffsetAndScoreAndId()
    }
  },
  getDynamicsByOffsetAndScoreAndId() {
    if (this.data.offset == null || '') {
      console.log('没有过呢更多啦')
    } else {
      app.request({
        url: app.globalData.apiUrl + '/dynamic/' + this.data.id + '/' + this.data.offset + '/' + this.data.score+'/'+this.data.userid,
        method: 'GET',
      }).then(res => {
        if (res.statusCode == 200) {
          this.setData({
            dynamics: res.data.data.list == null ? this.data.dynamics : this.data.dynamics.concat(res.data.data.list),
            offset: res.data.data.offset,
            score: res.data.data.minTime
          })
          console.log('score为' + this.data.score)
        } else {
					Notify('服务错误，退出重试');
        }
      })
    }
  },
  getCount() {
    app.request({
      url: app.globalData.apiUrl + "/follow/" + this.data.id,
      method: 'GET',
    }).then(res => {
      if (res.statusCode == 200) {
        this.setData({
          count: res.data.data
        })
        console.log(this.data.count)
      } else {
				Notify('服务错误，退出重试');
      }
    })
  },
  isFollowed() {
    app.request({
      url: app.globalData.apiUrl + "/follow/ed/" + this.data.id,
      method: 'GET',
    }).then(res => {
      if (res.statusCode == 200) {
        this.setData({
          followed: res.data.data
        })
        console.log(this.data.followed)
      } else {
				Notify('服务错误，退出重试');
      }
    })
  },
  //关注、取关
  attention() {
    var json = JSON.stringify({
      userId: this.data.userid,
      follow: this.data.id
    })
    console.log(json)
    app.request({
      url: app.globalData.apiUrl + "/follow/" + json,
      method: 'POST',
    }).then(res => {
      if (res.statusCode == 200) {
        this.setData({
          followed: true
        })
        Toast('关注成功');
      } else {
				Notify('服务错误，退出重试');
      }
    })
  },
  cencle() {
    var json = JSON.stringify({
      userId: this.data.userid,
      follow: this.data.id
    })
    app.request({
      url: app.globalData.apiUrl + "/follow/" + json,
      method: 'DELETE',
    }).then(res => {
      if (res.statusCode == 200) {
        this.setData({
          followed: false
        })
        Toast('取关成功');
      } else {
        Notify('服务错误，稍后重试');
      }
    })
	},
		//点赞
		giveALike(item) {
			var dynamic = item.target.dataset.item
			var index = item.target.dataset.index
			app.request({
				url: app.globalData.apiUrl + '/dynamic/dianzan/' + dynamic.id,
				method: 'POST',
			}).then(res => {
				if (res.statusCode == 200) {
					dynamic.liked = 1;
					dynamic.likes = dynamic.likes + 1;
					this.setData({
						['dynamics[' + index + ']']: dynamic
					})
				} else {
					Notify('服务错误，稍后重试');
				}
			})
		},
		//取消赞
		cancelLike(item) {
			var dynamic = item.target.dataset.item
			var index = item.target.dataset.index
			app.request({
				url: app.globalData.apiUrl + '/dynamic/cancel/' + dynamic.id,
				method: 'POST',
			}).then(res => {
				if (res.statusCode == 200) {
					dynamic.liked = 0;
					dynamic.likes = dynamic.likes - 1
					this.setData({
						['dynamics[' + index + ']']: dynamic
					})
				} else {
					Notify('服务错误，稍后重试');
				}
			})
		},
		gotoCommit(item){
			var dynamic = JSON.stringify(item.target.dataset.item)
			wx.navigateTo({
				url: '/pages/life/comment/comment?dynamic='+dynamic,
			})
		}
})